Welcome back. One sorting task that comes up frequently,
but it's a little tricky,
is sorting a dictionary.
The way we sort a dictionary,
is to sort its keys.
After that, we can iterate through the keys and look up values as we need to.
So, remember this code?
You've seen something like it before,
maybe with a slightly different list.
We've just got a list with a bunch of items.
Some of them are repeat and we're creating a dictionary that counts for every item.
For every distinct possible value in the list,
how many times does it come up in the list?
So, A comes up once, twice.
So, at the end in our dictionary D,
we're going to have the value two associated with the key A,
and then on lines nine and 10,
we're just printing out the values from that list.
So, E appears twice, F appears once,
A appears two times,
as I just manually countered and so on.
Notice that the keys E,
F, A, B, C and so on,
they appear not in any special order,
and that's just the way dictionaries work.
When you ask for the keys,
you get all the keys back but there's no promise made about what order they'll appear in.
Suppose I cared about the order,
and I really wanted to say A appears one times,
B appears twice and so on.
The way to do that is going to be on line nine
to sort the keys before doing the iteration.
So, let's suppose that instead of just asking for the keys,
I pass those keys into the sorted function.
Now, I am going to get the results in alphabetic order.
A appears two times,
B appears two times, and so on.
Well that's all well and good.
How about if we wanted to sort based on the counts instead?
So, we really want D to get printed out first because it appeared the most times,
and after that we'll get the things that appeared only twice.
Now, we can specify a property of these keys that we want to use for sorting.
There's a little confusion that's going to go on here
because we're using the word "key" in two different ways.
We use the word "key" to refer to a key in a dictionary.
Like in this dictionary D,
we have the letter capital A is a key and capital B as a key and so on.
Then we have a second use of the word "key"
which is the parameter name in the sorted function.
So, if we want to say,
sort these keys based on some property of them,
we say key equals and we pass in a function here.
These are just two different meanings
of the word" key" and you've got to keep them separated.
So, remember the key function is going to take one list item as an input.
In our case we have a list of all the keys A,
B, C, D, E,
F and so on, and so that's what we're going to have as one input,
and I'm going to call my function,
the parameter for this function K just to remind me that
the thing that's getting passed into it is one key from the dictionary,
like the letter A or the letter
F. I'm choosing the parameter named K to remind me of that,
and then we're going to return a property of that key.
The property we want is,
what is the value associated with that key in the dictionary D?
So, if I have the key C,
what I want to do is get the value one,
and I want to use that for the sort order.
For D, I want to use four.
The way I can do that,
is I refer to D square bracket K. Just look up for the current key,
what is its value in the dictionary and I get,
if the current key is C I'll get one,
and if the current t is D I'll get four.
This is really all I need to do in order to
re-sort this output in the way that I want it.
Let me just clear those markings for you.
So, now we have D appearing at the end because we're going lowest to highest,
the items that occur least frequently to the ones that occur most frequently.
If I wanted to do it in the reverse order,
I just use it reverse equals true,
like we've done before.
Now, D will get printed out first.
So, the things to remember here are,
we're not doing anything new with sorting.
There's no new mechanics here.
We're just passing in a function for the key parameter,
but we have, perhaps,
a little bit of a confusing function.
This function is taking one key from the dictionary as its input,
and returning a property of that key,
the look up of its value in the dictionary D. So,
we have key as in a key from the dictionary,
that's our letter K. We've chosen to remind us that we're dealing with a dictionary key,
and then we have the parameter name key for the sorted function.
One other thing I want to point out,
is that when we tell the sorted function to sort the keys,
there's a shorthand we can use.
You may recall we've said before that anytime a list is expected,
there's some place in the code where the Python interpreter is expecting a list.
If you provide a dictionary,
it will automatically grab all of the keys as the list.
So, this is equivalent.
We can either say D.keys or we can just say D,
because we're passing them to sorted.
If we pass the dictionary,
it automatically figures out that we want to sort the keys.
So, this is sorting a dictionary's keys based on their values.
There are several useful exercises at the bottom
of the page in the textbook that I encourage you to work
through in order to solidify your understanding. Will see you next time.